<?php
/*********
*    首页控制器
*/
namespace Home\Controller;
use Home\Controller\HomeController;
class AddressController extends HomeController {
 	public function _initialize(){
 		parent::_initialize();
 		
 	}
 	/**
 	 * 获取用户地址
 	 */
	public function getUserAddress(){
		$open_id = I('openid');
		$address = M('User_address')->where(array('open_id'=>$open_id))->select();
		//$province = M('Areas')->where(array('area_id'=>$address['areaid_1']))->select();
		//$city = M('Areas')->where(array('area_id'=>$address['areaid_2']))->select();
		//$county = M('Areas')->where(array('area_id'=>$address['areaid_3']))->select();
		//$address['allAddress'] = $province['area_name'].$city['area_name'].$county['area_name'];
		$this->ajaxReturn($address);
	}
	public function updateAddress(){
		$address_id = I('address_id');
		$open_id=I('openid');
		$arr['address_id']=$address_id;
		$arr['open_id']=$open_id;
		$address = M('user_address')->where($arr)->find();
			$p=M('areas');
			$t1['area_id']=$address['areaid_1'];
			$t2['area_id']=$address['areaid_2'];
			$t3['area_id']=$address['areaid_3'];
			$q=$p->where($t1)->find();
			$shi=$p->where($t2)->find();
			$qu=$p->where($t3)->find();
			$address[$k]['q']=$q['area_name'];
			$address[$k]['shi']=$shi['area_name'];
            $address[$k]['qu']=$qu['area_name'];
	
		$this->ajaxReturn($address);
	}
	public function updateAddress_do(){
		$open_id = I('openid');
		$username = I('username');
		$phone = I('phone');
		$areaid_1 = I('areaid_1');
		$areaid_2 = I('areaid_2');
		$areaid_3 = I('areaid_3');
		$address = I('address');
		$address_id = I('address_id');
		if(empty($username)){
			$this->ajaxReturn(['status'=>-1,'msg'=>'请输入收件人姓名']);
		}
		if(empty($phone)){
			$this->ajaxReturn(['status'=>-2,'msg'=>'请输入收件人电话']);
		}
		if(!checkMobile($phone)){
		    $this->ajaxReturn(['status'=>-22,'msg'=>'手机号码格式不正确']);
		}
		if(empty($areaid_1)){
			$this->ajaxReturn(['status'=>-3,'msg'=>'请选择省份']);
		}
		if(empty($areaid_2)){
			$this->ajaxReturn(['status'=>-4,'msg'=>'请选择城市']);
		}
		if(empty($areaid_3)){
			$this->ajaxReturn(['status'=>-5,'msg'=>'请选择地级县']);
		}
		if(empty($address)){
			$this->ajaxReturn(['status'=>-6,'msg'=>'请输入详细地址']);
		}
		$address_data = [
			'open_id'=>$open_id,
			'username'=>$username,
			'phone'=>$phone,
			'areaid_1'=>$areaid_1,
			'areaid_2'=>$areaid_2,
			'areaid_3'=>$areaid_3,
			'add_time'=>time(),
			'address'=>$address,
		];
		$r = M('User_address')->where(array('address_id'=>$address_id))->save($address_data);
		if(!$r){
			$this->ajaxReturn(['status'=>-1,'msg'=>'修改失败']);
		}
		$this->ajaxReturn(['status'=>1,'msg'=>'操作成功']);
	}
	public function addUserAddress(){
		$open_id = I('openid');
		$username = I('username');
		$phone = I('phone');
		$areaid_1 = I('areaid_1');
		$areaid_2 = I('areaid_2');
		$areaid_3 = I('areaid_3');
		$address = I('address');
		$address_id = I('address_id');
		if(empty($username)){
			$this->ajaxReturn(['status'=>-1,'msg'=>'请输入收件人姓名']);
		}
		if(empty($phone)){
			$this->ajaxReturn(['status'=>-2,'msg'=>'请输入收件人电话']);
		}
		if(!checkMobile($phone)){
		    $this->ajaxReturn(['status'=>-22,'msg'=>'手机号码格式不正确']);
		}
		if(empty($areaid_1)){
			$this->ajaxReturn(['status'=>-3,'msg'=>'请选择省份']);
		}
		if(empty($areaid_2)){
			$this->ajaxReturn(['status'=>-4,'msg'=>'请选择城市']);
		}
		if(empty($areaid_3)){
			$this->ajaxReturn(['status'=>-5,'msg'=>'请选择地级县']);
		}
		if(empty($address)){
			$this->ajaxReturn(['status'=>-6,'msg'=>'请输入详细地址']);
		}
		$address_data = [
			'open_id'=>$open_id,
			'username'=>$username,
			'phone'=>$phone,
			'areaid_1'=>$areaid_1,
			'areaid_2'=>$areaid_2,
			'areaid_3'=>$areaid_3,
			'add_time'=>time(),
			'address'=>$address,
		];
		if($address_id){
			$r = M('User_address')->where(array('address_id'=>$address_id))->save($address_data);
		}else{
			$r = M('User_address')->add($address_data);
		}
		if(!$r){
			$this->ajaxReturn(['status'=>-1,'msg'=>'服务器繁忙']);
		}
		$this->ajaxReturn(['status'=>1,'msg'=>'操作成功']);
	}
	public function setDefault(){
	    $open_id = I('openid');
		$address_id = I('address_id');
		$address_id_check = M('User_address')->where(array('address_id'=>$address_id))->find()['open_id'];
		if($open_id != $address_id_check){
		    $this->ajaxReturn(['status'=>-2,'msg'=>'服务器繁忙']);
		}
		if($address_id){
		    $r = M('User_address')->where(array('open_id'=>$open_id))->save(array('isDefault' =>0));
		    $r = M('User_address')->where(array('address_id'=>$address_id,'open_id'=>$open_id))->save(array('isDefault' =>1));
		}
		if(!$r){
		    $this->ajaxReturn(['status'=>-1,'msg'=>'服务器繁忙']);
		}
		$this->ajaxReturn(['status'=>1,'msg'=>'操作成功']);
	}
	public function delAddress(){
	    $open_id = I('openid');
		$address_id = I('address_id');
		$address_id_check = M('User_address')->where(array('address_id'=>$address_id))->find()['open_id'];
		if($open_id != $address_id_check){
		    $this->ajaxReturn(['status'=>-2,'msg'=>'服务器繁忙']);
		}
		if($address_id){
		    $r = M('User_address')->where(array('address_id'=>$address_id,'open_id'=>$open_id))->delete();
		}
		if(!$r){
		    $this->ajaxReturn(['status'=>-1,'msg'=>'服务器繁忙']);
		}
		$this->ajaxReturn(['status'=>1,'msg'=>'操作成功']);
	}
}
